﻿Imports System.Data.SqlClient

Public Class Security_Setup

    Private Sub BTN_Cancel_PW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Cancel_PW.Click
        Me.Close()
    End Sub

    Private Sub Security_Setup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LBL_AdminPW.Text = Admin_PW
        LBL_EditRec_PW.Text = EditRec_PW
        LBL_DelRec_PW.Text = DelRec_PW
        LBL_SecWordPW.Text = SecretWord_PW
    End Sub

    Private Sub BTN_Save_PW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Save_PW.Click
        'Store new passwords to the DB and assign the new passwords to the Global variables

        If TB_Admin_PW.Text = "" Then
            MsgBox("Admin cannot have a blank password." + vbCrLf + "Please enter a password for Admin and try again", MsgBoxStyle.Exclamation)
            GoTo Close
        ElseIf TB_SecretWord_PW.Text = "" Then
            MsgBox("Secret Word cannot have a blank password." + vbCrLf + "Please enter a password for Secret Word and try again", MsgBoxStyle.Exclamation)
            GoTo Close
        End If

        Try
            ESBD_Form.OpenSQLConnection()
            Dim connection As New SqlConnection(csb.ToString())
            cmd.Connection = connection
            connection.Open()

            cmd.CommandText = "UPDATE ""Security"" set Password = '" + TB_Admin_PW.Text + "' where Username = 'Admin'"
            cmd.ExecuteScalar()

            cmd.CommandText = "UPDATE ""Security"" set Password = '" + TB_EditRec_PW.Text + "' where Username = 'EditRec'"
            cmd.ExecuteScalar()

            cmd.CommandText = "UPDATE ""Security"" set Password = '" + TB_DelRec_PW.Text + "' where Username = 'DelRec'"
            cmd.ExecuteScalar()

            cmd.CommandText = "UPDATE ""Security"" set Password = '" + TB_SecretWord_PW.Text + "' where Username = 'SecretWord'"
            cmd.ExecuteScalar()
            connection.Close()

            'Assign New passwords to Global Vars
            SecretWord_PW = TB_SecretWord_PW.Text
            Admin_PW = TB_Admin_PW.Text
            EditRec_PW = TB_EditRec_PW.Text
            DelRec_PW = TB_DelRec_PW.Text

            'Assign the Labels the new passwords
            LBL_AdminPW.Text = Admin_PW
            LBL_EditRec_PW.Text = EditRec_PW
            LBL_DelRec_PW.Text = DelRec_PW
            LBL_SecWordPW.Text = SecretWord_PW

            'Set Passwords Entered Global Vars to N
            Admin_PW_Entered = "N"
            EditRec_PW_Entered = "N"
            DelRec_PW_Entered = "N"

            'Clear the Text boxes
            TB_EditRec_PW.Clear()
            TB_Admin_PW.Clear()
            TB_DelRec_PW.Clear()
            TB_SecretWord_PW.Clear()
            MsgBox("New Passwords Saved!", MsgBoxStyle.Information)
        Catch ex As Exception
            Dim connection As New SqlConnection(csb.ToString())
            connection.Close()
            MsgBox(ex.message)
        End Try
Close:
    End Sub
End Class